package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.evefeast.EveFeastGameData;
import com.yanqu.road.entity.activity.evefeast.UserEveFeastData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserEveFeastDaoImpl extends TempDataObjectDao<UserEveFeastData> {
    public static String getUserEveFeastDataSql = "select * from `t_u_activity_evefeast` where `activityId`=? and `userId`=?";
    public UserEveFeastData getUserEveFeastData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserEveFeastDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<UserEveFeastData> getNeedAutoUserEveFeastList(int activityId) {
        String sql = "select * from `t_u_activity_evefeast` where `activityId`=? and autoReward != ''";
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserEveFeastData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()){
                UserEveFeastData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateUserEveFeastDataSql = "update `t_u_activity_evefeast` set gameTimes=?,maxScore=?,maxScoreItems=?,buyToken1=?,buyToken2=?,gameData=?,autoReward=?,practiceData=?,guideStep=?,lastGameTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserEveFeastData userEveFeastData) {
        userEveFeastData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getGameTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getMaxScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userEveFeastData.getMaxScoreItems())));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEveFeastData.getBuyToken1()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEveFeastData.getBuyToken2()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userEveFeastData.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, userEveFeastData.getAutoReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userEveFeastData.getPracticeData())));
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getGuideStep()));
        para.put(i++, new DbParameter(Types.BIGINT, userEveFeastData.getLastGameTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEveFeastData.getUserId()));
        if (getDb().executeNoneQuery(updateUserEveFeastDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserEveFeastDataSql = "insert into `t_u_activity_evefeast`(activityId,userId,gameTimes,maxScore,maxScoreItems,buyToken1,buyToken2,gameData,autoReward,practiceData,guideStep,lastGameTime) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserEveFeastData userEveFeastData) {
        userEveFeastData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEveFeastData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getGameTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getMaxScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userEveFeastData.getMaxScoreItems())));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEveFeastData.getBuyToken1()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEveFeastData.getBuyToken2()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userEveFeastData.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, userEveFeastData.getAutoReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userEveFeastData.getPracticeData())));
        para.put(i++, new DbParameter(Types.INTEGER, userEveFeastData.getGuideStep()));
        para.put(i++, new DbParameter(Types.BIGINT, userEveFeastData.getLastGameTime()));
        if (getDb().executeNoneQuery(insertUserEveFeastDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserEveFeastData parseData(ResultSet rs) throws SQLException {
        UserEveFeastData userEveFeastData = new UserEveFeastData();
        userEveFeastData.setActivityId(rs.getInt("activityId"));
        userEveFeastData.setUserId(rs.getLong("userId"));
        userEveFeastData.setGameTimes(rs.getInt("gameTimes"));
        userEveFeastData.setMaxScore(rs.getInt("maxScore"));
        userEveFeastData.setMaxScoreItems(JSON.parseObject(rs.getString("maxScoreItems"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.evefeast.EveFeastItem>>() {}));
        userEveFeastData.setBuyToken1(rs.getBoolean("buyToken1"));
        userEveFeastData.setBuyToken2(rs.getBoolean("buyToken2"));
        userEveFeastData.setGameData(JSON.parseObject(rs.getString("gameData"), EveFeastGameData.class));
        String practiceData = rs.getString("practiceData");
        if (!StringUtils.isNullOrEmpty(practiceData)) {
            userEveFeastData.setPracticeData(JSON.parseObject(practiceData, EveFeastGameData.class));
        }
        userEveFeastData.setGuideStep(rs.getInt("guideStep"));
        userEveFeastData.setLastGameTime(rs.getLong("lastGameTime"));
        userEveFeastData.setAutoReward(rs.getString("autoReward"));

        return userEveFeastData;
    }
}
